package commonClasses;


import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.util.Version;

public class Index {

	public static void main(String[] args) throws Exception

	{

		String indexDir = "J:\\BC3_IMT_Training\\Index";
		// this is the path to folder where index directory is created

		String dataDir = "J:\\BC3_IMT_Training\\txt";
		// this is the path form where u get the files for indexing

		Index indexer = new Index(indexDir);

		long start = System.currentTimeMillis();

		int numIndexed = indexer.index(dataDir);

		indexer.close();

		long end = System.currentTimeMillis();

		System.out.println("Indexing " + numIndexed + " files took "

		+ (end - start) + " milliseconds");

	}

	private IndexWriter writer;

	public Index(String indexDir) throws IOException

	{
		try
		{
			FSDirectory dir = FSDirectory.open(new File(indexDir));

			StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);

			IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34,
					analyzer);

			writer = new IndexWriter(dir, config);

		}

		catch (Exception e)

		{
			System.out.print("no indexwriter");
		}

	}

	public void close() throws IOException

	{


		writer.close();

	}

	public int index(String dataDir) throws Exception

	{

		File[] files = new File(dataDir).listFiles();// gets the file if there
														// is subdirectory too

		System.out.println("Total files going to be indexed: " + files.length);

		for (int i = 0; i < files.length; i++)

		{

			try {

				File f = files[i];

				if (f != null)

				{

					if (!f.isDirectory() &&

					!f.isHidden()

					&& f.exists()

					&& f.canRead()

					&& f.length() > 0.0

					&& f.isFile()

					&& acceptFile(f))

					{

						System.out.println("Indexing file : "
								+ f.getAbsolutePath());

						indexFile(f);

					}

				}

			} catch (Exception e) {
				continue;
			}
		}

		return writer.numDocs();

	}

	protected boolean acceptFile(File f) throws Exception {
		return f.getName().endsWith(".txt"); // only return text documents
	}

	protected Document getDocument(File f) throws Exception {
		Document doc = new Document();
		doc.add(new Field("content", new FileReader(f),
				Field.TermVector.WITH_POSITIONS_OFFSETS));
		doc.add(new Field("filename", f.getName(), Field.Store.YES,
				Field.Index.ANALYZED));

		return doc;
	}

	private void indexFile(File f) throws Exception {
		System.out.println("Indexing " + f.getCanonicalPath());
		Document doc = getDocument(f);
		if (doc != null) {
			writer.addDocument(doc);
		}
	}

}